﻿using System.Web.Mvc;

namespace Krupa.Pro.Web.Utility.Composite
{
    public class RazorExtensionEngine : RazorViewEngine
    {
        
        public RazorExtensionEngine()
        {
            PartialViewLocationFormats = new[]
                                             {
                                                 "~/{1}/Composites/{0}.cmp"
                                                 , "~/Views/{1}/{0}.cshtml"
                                                 , "~/Views/{1}/{0}.vbhtml"
                                                 , "~/Views/Shared/{0}.cshtml"
                                                 , "~/Views/Shared/{0}.vbhtml"
                                             };
            ViewLocationFormats = new[]
                                      {
                                          "~/{1}/Composites/{0}.cmp"
                                          , "~/Views/{1}/{0}.cshtml"
                                          , "~/Views/{1}/{0}.vbhtml"
                                          , "~/Views/Shared/{0}.cshtml"
                                          , "~/Views/Shared/{0}.vbhtml"
                                      };
            FileExtensions = new[]
                                 {
                                     "cmp"
                                     , "cshtml"
                                     , "vbhtml"
                                 };
        }
        #region Overrides of VirtualPathProviderViewEngine

        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            if (partialPath.EndsWith(".cmp"))
            {
                return new CompositeView(controllerContext.HttpContext.Server.MapPath(partialPath));
            }
            return base.CreatePartialView(controllerContext, partialPath);
        }

        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            if (viewPath.EndsWith(".cmp"))
            {
                return new CompositeView(controllerContext.HttpContext.Server.MapPath(viewPath));
            }
            return base.CreateView(controllerContext, viewPath, masterPath);
        }

        #endregion
    }

}
